package dumpclipse;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.ui.ide.IDE;

public class Action implements IObjectActionDelegate {

	private IFile file;
	private String crlf = System.getProperty("line.separator");

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
	}

	@Override
	public void run(IAction action) {

		try {
			InputStream in = this.file.getContents();
			BufferedInputStream buf = new BufferedInputStream(in);
			MessageConsole console = new MessageConsole("MyConsole", null);
			MessageConsoleStream stream = console.newMessageStream();
			IConsoleManager manager = ConsolePlugin.getDefault()
					.getConsoleManager();
			manager.addConsoles(new IConsole[] { console });
			manager.showConsoleView(console);
			byte[] b = new byte[256];
			int len;
			while ((len = buf.read(b)) != -1) {
				String line = byteToString(b, len);
				stream.print(line);
			}

		} catch (IOException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		} catch (CoreException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}

		IWorkbenchPage page = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage();
		try {
			IDE.openEditor(page, this.file, true);
		} catch (PartInitException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}

	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof StructuredSelection) {
			StructuredSelection ss = (StructuredSelection) selection;
			// 選択は一つしかないはず
			Object obj = ss.getFirstElement();
			if (obj instanceof IFile)
				this.file = (IFile) obj;
		}
	}

	private String byteToString(byte[] b, int len) {

		StringBuffer s = new StringBuffer();
		for (int i = 0; i < len; i++) {
			Boolean crlfFlag = false;
			int d = b[i];
			d += d < 0 ? 256 : 0; // byte 128-255
			if (crlfFlag && d != 10) { // only CR
				s.append(this.crlf);
				crlfFlag = false;
			}
			if (d < 16) { // 0-f ... 00-0f
				s.append("0");
			}
			s.append(Integer.toString(d, 16));
			if (d == 13) { // first CR
				crlfFlag = true;
			}
			if (d == 10) { // CR + LF or only LF
				s.append(this.crlf);
				crlfFlag = false;
			} else {
				s.append(" ");
			}
		}
		return s.toString();
	}
}
